package com.wontube.smr.dao.bo;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.wontube.core.dao.mybatis.Criteria;
import com.wontube.core.dao.mybatis.QueryExample;
import com.wontube.smr.client.model.po.WebsiteLink;
import com.wontube.smr.dao.mapper.WebsiteLinkMapper;
import com.wontube.smr.pub.DefaultDBHelper;


/**
 * @author yuechao
 *
 */
public final class WebsiteLinkSelectDao {
	private WebsiteLinkSelectDao() {
	}
	
	public static final List<WebsiteLink> listDeviceHome(String sysId, String countryCode) {
		QueryExample ex = new QueryExample();
		ex.or().addCriterion("website_link.on_device_home = ", 1)
			.addCriterion("website_link.country_code = ", countryCode)
			.addCriterion("website_link.sys_id = ", sysId);
		ex.setOrderByClause("website_link.sort_index asc");
		
		return listItemsByExample(ex);
	}
	
	public static final List<WebsiteLink> listMoreItems(String sysId, String countryCode) {
		QueryExample ex = new QueryExample();
		ex.or().addCriterion("website_link.on_device_home = ", 0)
			.addCriterion("website_link.country_code = ", countryCode)
			.addCriterion("website_link.sys_id = ", sysId);
		ex.setOrderByClause("website_link.sort_index asc");
		
		return listItemsByExample(ex);
	}
	
	public static final List<WebsiteLink> listAllWebsiteLinks(String countryCode, String sysId, String category){
		QueryExample ex = new QueryExample();
		Criteria cri = ex.or().addCriterion("country_code=", countryCode);
		if(sysId != null){
			cri.addCriterion("sys_id=", sysId);
		}
		if(category != null){
			cri.addCriterion("category=", category);
		}
		ex.setOrderByClause("sys_id, sort_index asc");
		return listItemsByExample(ex);
	}	
	
	public static final List<WebsiteLink> listWebsiteLinks(String sysId){
		QueryExample ex = new QueryExample();
		ex.or().addCriterion("sys_id=", sysId);		
		ex.setOrderByClause("sort_index asc");
		return listItemsByExample(ex);
	}	
	
	public static final boolean updateWebsiteLinkByPK(WebsiteLink website){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try{
			WebsiteLinkMapper mapper = session.getMapper(WebsiteLinkMapper.class);
			int aff = mapper.updateByPrimaryKey(website);
			return aff == 1;
		}finally{
			session.close();
		}
	}
	public static final WebsiteLink getWebsiteLinkByPK(String pk){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try{
			WebsiteLinkMapper mapper = session.getMapper(WebsiteLinkMapper.class);
			WebsiteLink website = mapper.selectByPrimaryKey(pk);
			return website;
		}finally{
			session.close();
		}
	}
	
	public static boolean saveOrUpdateWebsiteLink(WebsiteLink website){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			WebsiteLinkMapper mapper = session.getMapper(WebsiteLinkMapper.class);
			WebsiteLink temp = mapper.selectByPrimaryKey(website.getId());
			int aff = 0;
			if(temp != null){
			 aff = mapper.updateByPrimaryKey(website);
			}else{
			 aff = mapper.insert(website);
			}
			return aff == 1;
		} finally {
			session.close();
		}
	}
	
	public static boolean deleteWebsiteLink(String pk){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			WebsiteLinkMapper mapper = session.getMapper(WebsiteLinkMapper.class);
			int aff = mapper.deleteByPrimaryKey(pk);
			return aff == 1;
		} catch (Exception e) {
			return false;
		}finally{
			session.close();
		}
	}
	
	private static final List<WebsiteLink> listItemsByExample(QueryExample ex) {
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			WebsiteLinkMapper mapper = session.getMapper(WebsiteLinkMapper.class);
			return mapper.selectByExample(ex);
		} finally {
			session.close();
		}
	}
}
